Borland Online And The Cobb Group Present:


September, 1994 - Vol. 1 No. 9

Letters - Using the TArrayAsVector class's ArraySize( ) member function

I'm using the BIDS classes in Borland C++ 4.0, and I'm seeing a strange problem in my program. When I create an array using the TArrayAsVector template class, the ArraySize() member function doesn't seem to consistently return the correct number of items in the array.

Is this a bug in the TArrayAsVector class? If so, is there another way to determine the number of elements the array currently holds?

Paula Johnstone

Lexington, Kentucky

Paula, this isn't a bug in the TArrayAsVector class­­instead, it's a simple case of mistaken identity between the member functions ArraySize() and GetItemsInContainer(). To see what's happening, take a look at the simple program that appears in Figure A.


Figure A - The ArraySize( ) member function returns the size of the array object - not the number of items in the array.

#include <arrays.h>
#include <iostream.h>

int main()
{
  TArrayAsVector<short> i(3, 0, 4);

  i.Add(1); 
  i.Add(2); 
  i.Add(3); 
  i.Add(4); 
  i.Add(5); 
  i.Add(6);

  for(int count = 0;
      count < i.ArraySize();
      ++count)
  {
    cout << i[count]; << " ";

  return 0; 
}

If you enter and run this program, you'll see output similar to the following:

1 2 3 4 5 6 0 0

You'll definitely see the first six numbers. However, you may or may not see zeros as the last two numbers, depending on what values happen to be in memory at the time.

The ArraySize() function returns the value 8, because we created the array using the parameters 3, 0, and 4 in the constructor. The 3 represents the last index of the array initially (upper bound), the 0 represents the first index of the array (lower bound), and the 4 represents the number of elements the program will add to the array if it grows past the last index (delta).

In our program, the value the function ArraySize() would initially return is 4 (an array with 0 through 3 indices). However, as soon as we add the fifth item, the array allocates space for four more values. Figure B shows a simplified picture of how the compiler arranges the elements of array i in memory after we've added the fifth and sixth elements.


Figure B - If you add more elements to the array than you initially allocated, the array may have empty elements.

To avoid this problem, you can instead call the GetItemsInContainer() member function. This function will return the number of elements you've actually put into the array. Accordingly, if you change the line

count < i.ArraySize();

to

count < i.GetItemsInContainer();

the for loop will work correctly.

Return to the Borland C++ Developer's Journal index

Subscribe to the Borland C++ Developer's Journal


Copyright (c) 1996 The Cobb Group, a division of Ziff-Davis Publishing Company. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff-Davis Publishing Company is prohibited. The Cobb Group and The Cobb Group logo are trademarks of Ziff-Davis Publishing Company.